//
//  SearchDetailsViewController.m
//  项目A --半糖
//
//  Created by romana on 16/6/1.
//  Copyright © 2016年 romana. All rights reserved.
//

#import "SearchDetailsViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "SecondTabHotModel.h"
#import "SecondTabHotCollectionViewCell.h"
#import "SecondTabHotDetailsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface SearchDetailsViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation SearchDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.font = [UIFont systemFontOfSize:20];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"礼物";
    self.navigationItem.titleView = titleLabel;
    
    
    // collectionView
    [self createCollectionView];
    // 数据处理
    self.dataArr = [NSMutableArray array];
    [self getData];
    
   // self.view.backgroundColor = [UIColor yellowColor];
    // Do any additional setup after loading the view.
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((WIDTH - 20 - 10) / 2, (HEIGHT - 64 - 49 - 30) / 2 - 10);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,  0, WIDTH, HEIGHT - 20 - 64 - 49) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[SecondTabHotCollectionViewCell class] forCellWithReuseIdentifier:@"collectionCell"];
    
}


-(void)getData{

    NSString *formatStr = [self.searchName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
    
    NSString *str = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/search/post?limit=20&offset=0&sort=&keyword=%@",formatStr];
    
    //若直接点搜索  弹出alert
    if ([str isEqualToString:@"http://api.liwushuo.com/v2/search/post?limit=20&offset=0&sort=&keyword="]) {
        UIAlertController *al = [UIAlertController alertControllerWithTitle:@"暂无您的礼物" message:@"请正确输入" preferredStyle:(UIAlertControllerStyleAlert)];
        [self presentViewController:al animated:YES completion:^{
            [self dismissViewControllerAnimated:YES completion:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }];
        
    }
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dataDic = responseObject[@"data"];
        NSArray *postsArr = dataDic[@"posts"];
        
        if (0 == postsArr.count) {
            UIAlertController *al = [UIAlertController alertControllerWithTitle:@"暂无您的礼物" message:@"请正确输入" preferredStyle:(UIAlertControllerStyleAlert)];
            [self presentViewController:al animated:YES completion:^{
                [self dismissViewControllerAnimated:YES completion:^{
                    [self.navigationController popViewControllerAnimated:YES];
                }];
            }];
            
        }
        
        for (NSDictionary *dic in postsArr) {
            SecondTabHotModel *h = [[SecondTabHotModel alloc] init];
            [h setValuesForKeysWithDictionary:dic];
            [self.dataArr addObject:h];
            
        }
        [self.collectionView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];



}


-(void)viewWillAppear:(BOOL)animated{
    [self.collectionView reloadData];
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondTabHotDetailsViewController *detailVC = [[SecondTabHotDetailsViewController alloc] init];
    SecondTabHotModel *h = self.dataArr[indexPath.row];
    //    NSLog(@"h.url   %@",h.url);
    detailVC.myUrl = h.url;
    detailVC.nameStr = self.searchName;
    [self.navigationController pushViewController:detailVC animated:YES];
    //    [detailVC release];
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondTabHotCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionCell" forIndexPath:indexPath];
    SecondTabHotModel *h = self.dataArr[indexPath.row];
    
    cell.nameLable.text = h.title;
    [cell.nameLable sizeToFit];
    cell.priceLable.text = h.price;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:h.cover_image_url]placeholderImage:[UIImage imageNamed:@"aea4d2a2777f9aaff6153414cc37838e"]];
    NSString *str = [NSString stringWithFormat:@"%@",h.likes_count];
    cell.likeLable.text = str;
    
//    cell.likeBtn.selected = [[DataBaseManager shareIndtance] selectHotByTitle:h.title];
    
    cell.url = h.url;
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}




@end
